package controller

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"mock/lib/font/config"
	. "mock/util"

	uuid "github.com/satori/go.uuid"
)

func Index(c *gin.Context) {
	c.HTML(http.StatusOK, "index.tmpl", nil)
	// c.JSON(http.StatusOK, gin.H{
	// 	"author": "yushanma",
	// 	"page":   "index",
	// 	"usage":  "请求示例 curl 127.0.0.1:8080/GetImg/:worlds/:horV/:font ",
	// 	"param":  "其中worlds为字符;horV为H水平/V为垂直;font为font1/font2两种字体",
	// })
}

// 参数 worlds,horV,font => 春联内容，方向，字体
func GetImg(c *gin.Context) {

	// ================== 参数处理
	worlds := c.PostForm("worlds")
	// 处理字符
	var charset []string
	if len(worlds) > 0 {
		for _, v := range worlds {
			charset = append(charset, string(v))
		}
	} else {
		charset = []string{"你", "好", "世", "界"}
	}

	// 处理方向
	horV := c.PostForm("horV")
	if len(horV) > 0 {
		if horV != "H" && horV != "V" {
			horV = "H"
		}
	} else {
		horV = "H"
	}

	// 输出路径
	outPath := "../res/" + uuid.NewV4().String() + ".png"

	// 处理字体
	font := config.Font_1
	ft := c.PostForm("font")
	if len(ft) > 0 {
		if ft != "font1" && ft != "font2" {
			font = config.Font_1
		}
	} else {
		font = config.Font_1
	}

	// 生成对联
	Generator(charset, outPath, horV, font)

	// 返回数组
	urls := []string{outPath}

	c.JSON(http.StatusOK, gin.H{
		"status": "success",
		"urls":   urls,
	})
}
